<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="query.real_name" placeholder="客户姓名" style="width: 130px;margin-top:8px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="query.pay_status" placeholder="付款状态" class="filter-item" style="width:130px;margin-top:8px">
        <el-option label="付款状态" value="" />
        <el-option label="已付" value="1" />
        <el-option label="未付" value="2" />
      </el-select>
      <el-select v-model="query.merchant" placeholder="选择商户" filterable class="filter-item" style="width:130px;margin-top:6px">
        <el-option label="请选择商户" value="" />
        <el-option
          v-for="item in merchant_list"
          :key="item.merchant_id"
          :label="item.merchant_name"
          :value="item.merchant_id"
        />
      </el-select>
      <el-select v-model="query.city" placeholder="选择城市" filterable class="filter-item" style="width:130px;margin-top:8px" @change="getRegionList">
        <el-option label="请选择城市" value="" />
        <el-option
          v-for="item in city"
          :key="item.city"
          :label="item.name"
          :value="item.city"
        />
      </el-select>
      <el-select v-model="query.b_model" placeholder="保理模式" class="filter-item" style="width:130px;margin-top:6px">
        <el-option label="保理模式" value="" />
        <el-option label="正常" value="1" />
        <el-option label="买断" value="2" />
        <el-option label="绿通" value="3" />
      </el-select>
      <el-date-picker v-model="date1" style="width:150px;" format="yyyy-MM-dd" type="datetime" placeholder="订单开始日期" />
      <el-date-picker v-model="date2" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="订单结束日期" />
      <el-date-picker v-model="date3" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="付款开始日期" />
      <el-date-picker v-model="date4" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="付款结束日期" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:8px" @click="handleFilter">查询</el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:8px" @click="excelout">导出</el-button>

      <!-- <excelout /> -->
    </div>

    <!-- <el-row style="font-size:14px;color:#666">
        <el-col :span="4">已付金额：{{ counts.paid_money }}</el-col>
    </el-row> -->

    <el-table :key="tableKey" v-loading="listLoading" :data="list" size="mini" border fit highlight-current-row style="width: 100%;margin-top:20px">

      <el-table-column label="客户名称" align="center" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.real_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="模式" align="center" width="50px">
        <template slot-scope="{row}">
          <span>{{ row.model }}</span>
        </template>
      </el-table-column>
      <el-table-column label="医院名称" align="center" width="120px">
        <template slot-scope="{row}">
          <span>{{ row.sort_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款账号" align="center" width="160px">
        <template slot-scope="{row}">
          <span>{{ row.bank_number }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开户地址" align="center" width="200px">
        <template slot-scope="{row}">
          <span>{{ row.bank_address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款人" align="center" width="210px">
        <template slot-scope="{row}">
          <span>{{ row.pay_person }}</span>
        </template>
      </el-table-column>

      <el-table-column label="订单时间" align="center" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付金额" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.total_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已付金额" align="center" width="100px">
        <template slot-scope="{row}">
          <div class="input-box">
            <el-input v-model="row.paid_money" size="small" prop="paid_money" :disabled="row.is_paid==1?true:false" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="未付金额" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.total_money-row.paid_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款状态" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.status_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款时间" align="center" width="140px">
        <template slot-scope="{row}">
          <div v-if="row.is_paid==0&&row.paid_time==''&&row.paid_money!=0" class="block">
            <el-date-picker
              v-model="value1"
              style="width:120px"
              format="yyyy-MM-dd"
              type="datetime"
              placeholder="选择日期时间"
              @change="checkthis(row.pay_id)"
            />
          </div>
          <span v-if="row.is_paid==1">{{ row.paid_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款给渠道" class-name="status-col" width="90">
        <template slot-scope="{row}">
          <el-switch v-if="row.apy_channel==0" value="row.enforce" @change="channel(row)" />
          <span v-else style="cursor:pointer;color:#46A6FF" @click="channel(row)">{{ row.payee }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template slot-scope="{row,$index}">
          <el-button v-if="row.is_paid!=1" type="primary" size="mini" @click="onSubmit(row)">
            保存
          </el-button>
          <el-button v-if="row.is_paid==1" type="default" size="mini" @click="updateFirst(row)">
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="query.page" :limit.sync="query.limit" @pagination="getList" />
    <el-dialog title="渠道信息" :visible.sync="channelAble" style="width:1000px;margin:auto">
      <el-form ref="dataForm" :model="form" label-width="120px">
        <el-form-item label="收款人">
          <el-input v-model="form.payee" />
        </el-form-item>
        <el-form-item label="收款账户">
          <el-input v-model="form.account" />
        </el-form-item>
        <el-form-item label="开户行">
          <el-input v-model="form.bank" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="channelSubmit()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改首打款" :visible.sync="firstAble" style="width:1000px;margin:auto">
      <el-form ref="dataForm" :model="firstform" label-width="120px">
        <el-form-item label="付款金额">
          <el-input v-model="firstform.paid_money" type="number" />
        </el-form-item>
        <el-form-item label="付款时间">
          <div  class="block">
            <el-date-picker
              v-model="firstform.settle_date"
              style="width:200px"
              format="yyyy-MM-dd"
              type="datetime"
              placeholder="选择日期时间"
            />
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateFirstSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getMerchantFirst, paymentMerchantEdit, merchantFirstExcelout, getRegion, channelSubmit,backendgetmerchantregion,updateFirstSubmit
} from '@/api/finance'
import excelout from '@/components/excelout/index.vue'
import { formatDate } from '@/utils/formatDate.js'
import { merchantList, merchantListOnlyMerchant } from '@/api/merchant'
import { fetchList } from '@/api/sale'
import waves from '@/directive/waves' // waves directive
import {
  parseTime
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {
    Pagination, excelout
  },
  directives: {
    waves
  },
  filters: {},
  data() {
    return {
      form: {
        payee: '',
        account: '',
        pay_id: '',
        bank: ''
      },
      firstform: {
        pay_id:'',
        order_id:'',
        paid_money:'',
        settle_date:''
      },
      firstAble:false,
      counts: [],
      channelAble: false,
      tableKey: 0,
      tableRecord: 1,
      list: null,
      order: [],
      project: '',
      record_list: null,
      listRecordLoading: true,
      recordTotal: 0,
      total: 0,
      listLoading: true,
      city: [],
      query: {
        pay_status: '',
        page: 1,
        city: '',
        limit: 10,
        real_name: '',
        merchant: '',
        b_model: ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      listMerchantQuery: {
        page: 1,
        limit: 500
      },
      merchant_list: [],
      value1: '',
      value2: '',
      date1: '',
      date2: '',
      date3: '',
      date4: '',
      checkthisval: ''
    }
  },
  created() {
    this.cacheQueryFormat()
    this.getList()
    this.getRegionList()
    // 添加钩子，页面刷新
    window.addEventListener('beforeunload', e => this.beforeunloadFn(e))
    this.getMerchantList()
  },
  mdestroyed() {
    // 添加钩子，页面刷新
    window.removeEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  methods: {
    channel(row) {
      this.channelAble = true
      this.form.pay_id = row.pay_id
      this.form.payee = row.payee
      this.form.account = row.account
      this.form.bank = row.bank
    },
    channelSubmit() {
      if (this.form.payee == '' || this.form.payee == null) {
        this.$notify({
          title: '提示',
          message: '收款人必填',
          type: 'error',
          duration: 2000
        })
        return
      }
      channelSubmit(this.form).then(response => {
        this.$notify({
          title: '提示',
          message: '添加成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
        this.channelAble = false
      })
    },
    updateFirstSubmit() {
      paymentMerchantEdit(this.firstform).then(response => {
        this.$notify({
          title: '提示',
          message: '成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
        this.firstAble = false
      })
    },
    updateFirst(row){
      this.firstform.order_id = row.order_id;
      this.firstform.pay_id = row.pay_id;
      this.firstform.paid_money = row.paid_money;
      this.firstform.settle_date = row.paid_time;
      this.firstAble = true;
    },
    beforeunloadFn(e) {
      // 刷新页面事件
      sessionStorage.removeItem('finacefirst')
    },
    cacheQueryFormat() {
      const cacheQuery = sessionStorage.getItem('finacefirst')
      if (cacheQuery) {
        this.query = JSON.parse(cacheQuery)
      }
    },
    getRegionList() {
      backendgetmerchantregion(this.listQuery).then(response => {
        if (this.query.city) {
          this.quyu = response.data
        } else {
          this.city = response.data
        }
      })
    },
    getList() {
      sessionStorage.setItem('finacefirst', JSON.stringify(this.query))
      getMerchantFirst(this.query).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        // this.counts = response.data.list.counts;
        // this.project = response.data.project;
        this.listLoading = false
      })
    },
    getMerchantList() {
      merchantListOnlyMerchant(this.listMerchantQuery).then(response => {
        this.merchant_list = response.data.merchant.data
      })
    },
    handleFilter() {
      this.query.page = 1
      this.query.date1 = this.date1
      this.query.date2 = this.date2
      this.query.date3 = this.date3
      this.query.date4 = this.date4
      this.getList()
    },
    checkthis(id) {
      console.log(id)
      this.checkthisval = id
    },
    excelout() {
      this.listLoading = true
      this.query.page = 1
      this.query.limit = this.total
      merchantFirstExcelout(this.query).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },
    onSubmit(row) {
      const pam = {}
      pam.paid_money = row.paid_money
      pam.pay_id = row.pay_id
      pam.settle_date = this.value1

      paymentMerchantEdit(pam).then(resp => {
        this.$notify({
          title: '提示',
          message: '修改成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
      })
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }
  select{
    width:100px;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .tb-edit .input-box {
    display: none
  }
  .tb-edit .current-cell .input-box {
    display: block;
    margin-left: -15px;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    float: left;
  }
</style>
